import { Body, Controller, Get, HttpStatus, Param, ParseIntPipe, Post, Query, Req, Res } from '@nestjs/common';
import { ChargeOrderService } from './charge-order.service';
import { UtilsService } from '@app/utils';

@Controller('chargeOrder')
export class ChargeOrderController {
    constructor(
        private readonly service:ChargeOrderService,
        private readonly utils:UtilsService
    ){}

    @Get('page')
    async page(@Query() query:Pagination, @Req() { userId }) {
        let rows = await this.service.page(query, userId)
        return this.utils.successRes(rows)
    }

    @Get('charging')
    async charging(@Query() querys:{init?:number}, @Req() { userId }) {
        const res = await this.service.charging(userId, Number(querys.init || 0))
        return this.utils.successRes(res)
    }

    @Get('detail/:id')
    async detail(@Param('id', ParseIntPipe) id:number, @Req() { userId }) {
        if(isNaN(id) || id < 1) return
        const res = await this.service.detail(id, userId)
        return this.utils.successRes(res)
    }

    @Post('params')
    async params(@Body() dto:{id:number, pageNo:number, pageSize:number}) {
        const res = await this.service.params(dto.id, dto.pageNo, dto.pageSize)
        return this.utils.successRes(res ? res.data : null)
    }

    //免密支付，充电通知
    @Post('payscore/notify/:client/:id')
    async payscoreNotify(@Param('id', ParseIntPipe) id:number, @Param('client') client:string, @Body() data:any, @Res() res) {
        if(isNaN(id) || id < 1) return

        try {
            if(client == 'wx') {
                if(data.event_type == 'PAYSCORE.PAYSCORE.USER_PAID') {
                    const r = await this.service.payscoreNotify(id, data.resource, true)
                    console.log(r)
                    if(r) {
                       res.status(HttpStatus.OK).send();
                    }else {
                        res.status(HttpStatus.NOT_IMPLEMENTED).send({code : 'FAIL', message : "失败"});
                    }
                }     
                
            }else {
    
            }

            return
        } catch (error) {
            
        }
        
        res.status(HttpStatus.NOT_IMPLEMENTED).send({code : 'FAIL', message : "失败"});
    }
}
